﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Collections.Specialized;
//using xml = System.Xml.Serialization;
//using System.Globalization;
//using System.IO;
//using System.Xml;
//using System.Xml.Serialization;

//namespace Framework.Serializer
//{
//    class XmlSerializer : StringSerializeBase
//    {
//        static XmlSerializerFactory factory = new XmlSerializerFactory();
//        /// <summary>
//        /// 
//        /// </summary>
//        /// <param name="obj"></param>
//        /// <returns></returns>
//        public override string Serialize(object obj)
//        {
//            if (obj != null)
//            {
//                xml.XmlSerializer xs = factory.CreateSerializer(obj.GetType());
//                using (MemoryStream stream = new MemoryStream())
//                {
//                    XmlWriterSettings setting = new XmlWriterSettings();
//                    setting.Encoding = new UTF8Encoding(false);
//                    using (XmlWriter writer = XmlWriter.Create(stream, setting))
//                    {
//                        xs.Serialize(writer, obj);
//                    }
//                    return Encoding.UTF8.GetString(stream.ToArray());
//                }
//            }
//            return string.Empty;
//        }


//        public override T DeSerialize<T>(string source)
//        {
//            if (!source.IsBlank())
//            {
//                xml.XmlSerializer xs = factory.CreateSerializer(typeof(T));
//                using (StringReader reader = new StringReader(source))
//                {
//                    XmlReader xr = XmlReader.Create(reader);
//                    return (T)xs.Deserialize(xr);
//                }
//            }
//            return default(T);
//        }
//    }
//}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using xml = System.Xml.Serialization;
using System.Globalization;
using System.IO;
using System.Xml;

namespace Framework.Serializer
{
    class XmlSerializer : StringSerializeBase
    {
        #region xml.XmlSerializer Dictionary

        private static Dictionary<Type, xml.XmlSerializer> dict = new Dictionary<Type, xml.XmlSerializer>();

        static xml.XmlSerializer GetSerializer(Type t)
        {
            xml.XmlSerializer serializer;
            if (!dict.TryGetValue(t, out serializer))
            {
                serializer = new xml.XmlSerializer(t);
                dict[t] = serializer;
            }
            return serializer;
        }
        #endregion


        public override string Serialize(object obj)
        {
            if (obj != null)
            {
                xml.XmlSerializer xs = GetSerializer(obj.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlWriterSettings setting = new XmlWriterSettings();
                    setting.Encoding = new UTF8Encoding(false);
                    using (XmlWriter writer = XmlWriter.Create(stream, setting))
                    {
                        xs.Serialize(writer, obj);
                    }
                    return Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            return string.Empty;
        }


        public override T DeSerialize<T>(string source)
        {
            if (!source.IsBlank())
            {
                xml.XmlSerializer ser = GetSerializer(typeof(T));
                using (StringReader reader = new StringReader(source))
                {
                    XmlReader xr = XmlReader.Create(reader);
                    return (T)ser.Deserialize(xr);
                }
            }
            return default(T);
        }
    }
}
